
  <style type="text/css">
  	body{padding: 10px 30px;}
  	.hide{display:none}
  </style>


		<blockquote class="layui-elem-quote">注意：如果当前修改的管理员为系统拥有者，不可取消任何权限，因为拥有者默认开通全部权限</blockquote>

		
		<table class="layui-table layui-form"  id="tree-table"></table>

		<script>
			layui.config({
				base: '/static/layui_extends/', 
			})
			layui.use(['treetable','iconPicker','form','laytpl'],function(){
				var data= {$data};
				var $ = layui.$;
				var form = layui.form,layer = layui.layer,laytpl = layui.laytpl,iconPicker=layui.iconPicker;
				//var treetable ='';
				var layer_index;
				var aid = '{:input('param.aid')}';
				
				if(aid==1){
					layer.alert('您好，当前设置的用户为网站拥有者，默认拥有所有权限，改变任何权限都对其无效！');
				}
				for(var k in data){
					data[k].name= data[k].pid!=0?"&nbsp;&nbsp;&nbsp;&nbsp;"+data[k].name:'<a style="color:#01AAED">'+data[k].name+'</a>';
				}
				treetable_render(data);
			
				//js渲染页面状态栏
				var nav_bar=[
				{"type":"menu","url":"/index/adminlist.html"},
				{"type":"url","url":"{:url('adminlist')}","title":"系统用户"},
				{"type":"url","url":"","title":"修改权限-{:input('param.name')}"},
				]
				enianAdmin.render_nav_bar(nav_bar);
			
				form.on('switch(pass)',function(data){
					//layer.msg('监听权限操作');
					var id = $(this).attr('value');
					var pid = $(this).data('pid');
					var url = '';
					var need_update=[];
					if(pid==0){
						var group_elem=$('#tree-table input[data-pid="'+id+'"]');
						
						group_elem.each(function(){
							need_update.push($(this).attr('value'));
						})
						
						if(data.elem.checked){
							$(group_elem).prop('checked','1')
							url="{:url('setAdminAuth',['t'=>'data'])}?type=1&aid={:input('param.aid')}";
						}else{
							url="{:url('setAdminAuth',['t'=>'data'])}?type=2&aid={:input('param.aid')}";
							$(group_elem).removeAttr('checked')
						}
						console.dir($(' input[data-pid="'+id+'"]'))
						form.render();
						
					}else{
						need_update.push($(this).attr('value'));
						if(data.elem.checked){
							url="{:url('setAdminAuth',['t'=>'data'])}?type=1&aid={:input('param.aid')}";
						}else{
							url="{:url('setAdminAuth',['t'=>'data'])}?type=2&aid={:input('param.aid')}";
						}
					}
					
					//请求
					$.ajax({
						type:"post",
						data:{data:need_update},
						url:url,
						async:true,
						success:function(e){
							// console.log(e.msg)
							if(e.code==1){
								//layer.msg()
							}else{
								layer.alert('操作失败，请稍后再试');		
							}
						}
					});
				})

				
				treetable.on('treetable(edit)',function(d){
					layer.msg('编辑操作');
				
				})
				
				//添加新菜单
				$('.up-all').click(function(){
					var d = {'type':'addMenu'};
					editWindow("#setMenuLeft_addMenuTpl",d)
					form.render()
				})
				
				$('.down-all').click(function(){
					treetable.all('down');
				})
				
				$('#setMenuLeft_btn_del_check').click(function(){
					var  checked =treetable.all('checked');
					// console.log(treetable.all('checked'))
					if(checked.ids.length==0){
						layer.msg('请先勾选需要删除的数据')
						return;
					}
					layer.confirm('确定要删除所有选中的数据吗？',function(){
						$.ajax({
							type:"post",
							url:"{:url('setMenuLeft',['t'=>'data'])}?type=5",
							data:{ids:checked.ids},
							async:true,
							success:function(e){
								if(e.code!=0){
									//treetable_reload()
									layer.msg('删除成功的个数为'+e.count+'个');
								}else{
									layer.msg('删除失败');
								}
							}
						});
					})
				})
				
				$('.get-checked').click(function(){
					console.dir(treetable.all('checked'));
				})

				form.on('submit(setMenuLeft_addMenu_submit)',function(data){
					// console.log($(this).data('type'))
					var type=$(this).data('type');
					if(type=='addMenu'){
						type =1;
					}else if(type=='addSonMenu'){
						type =2;
					}else if(type=='updateMenu'){
						type =3;
					}else if(type=='updateSonMenu'){
						type =4;
					}
					$.ajax({
						type:"post",
						url:"{:url('setMenuLeft',['t'=>'data'])}?type="+type,
						data:data.field,
						async:true,
						success:function(e){
							if(e.code!=0){
								layer.msg('操作成功');
								treetable_reload()
							}else{
								layer.msg('操作失败');
							}
						}
					});
					
					console.dir(data);
					return false;
				})
				//listing_order
				
				//回车事件绑定
			    $('.listing_order').bind('keypress', function(event) {
			        if (event.keyCode == "13") {   
			        	var that =$(this);
			        	//$(this).data('id')
			        	//$(this).val()
			        	// console.log(that.val())
			            $.ajax({
							type:"post",
							url:"{:url('setMenuLeft',['t'=>'data'])}?type=7",
							data:'id='+that.data('id')+'&field=order&value='+that.val(),
							async:true,
							success:function(e){
								if(e.code==1){
									layer.msg('修改成功')
									that.blur()
								}else{
									layer.msg('修改失败')
									treetable_reload()
								}
							}
						});
			        }
			    });

				//-------------------------------------------------------------------------
				//重新加载菜单
				function treetable_reload(){
					layer.close(layer_index)
					window.location.reload()
//					$.ajax({
//							type:"post",
//							url:"{:url('setMenuLeft',['t'=>'data'])}?type=6",
//							//data:{ids:checked.ids},
//							async:true,
//							success:function(e){
//								treetable_render(e.data);
//							}
//						});
				}
				
				
				//渲染菜单
				function treetable_render(data){
					treetable = layui.treetable;
					treetable.render({
						elem: '#tree-table',
						data: data,
						field: 'name',
						//is_checkbox: true,
						//checked:[1],
						/*icon_val: {
							open: "&#xe619;",
							close: "&#xe61a;"
						},
						space: 4,*/
						cols: [
							{
								// field: 'order',
								title: '权限',
								width: '8%',
								template: function(item){
									if(item.pass==1){
										return '<input type="checkbox"  lay-skin="switch" lay-filter="pass" data-pid='+item.pid+' value='+item.id+' lay-text="ON|OFF" checked>';
									}else{
										return '<input type="checkbox"  lay-skin="switch" lay-filter="pass" data-pid='+item.pid+' value='+item.id+' lay-text="ON|OFF">';
									}
								}
								
							},
							{
								field: 'name',
								title: '子/应用名称',
								width: '30%',
//								template: function(item){
//									return item.pid==0?item.name:'<a style="color:red">'+item.name+'</a>';
//									//return '<input type="text"  data-id="'+item.id+'" lay-filter="order" value="'+item.order+'" class="layui-input listing_order" style="width:50px">'
//								}
							},
//							{
//								//field: 'img',
//								title: '图标',
//								width: '5%',
//								template: function(item){
//									return '<i class="layui-icon '+item.img+'"></i>   '
//								}
//								
//							},
							{
								field: 'note',
								title: '备注',
								width: '20%'
							},
//							{
//								title: '类型',
//								width: '10%',
//								template: function(item){
//									var msg='';
//									
//									if(item.type==0){
//										msg='内部打开'
//									}else if(item.type==1){
//										msg='新窗打开'
//									}
//									return msg;
//								}
//							},
							
							{
								field: 'url',
								title: '地址',
								width: '25%',
							},
//							{
//								field: 'actions',
//								title: '操作',
//								width: '30%',
//								template: function(item){
//									
//									
//
////									if(item.id == 'g_0'){
////										return '<a class="add-child" lay-filter="add" style="cursor: pointer;">添加核心权限</a>';
////									}
//									//tem.push('<a lay-filter="edit" style="cursor: pointer;">编辑</a>');
//									//return tem.join(' <font>|</font> ')
//								},
//							},
						]
					});
			
				}
				
				//加载编辑窗口
				function editWindow(elem,d,oldData){
					
					laytpl($(elem).html()).render(d, function(html){
						layer_index=layer.open({
						  type: 1,
						  skin: '', //加上边框
						  area: ['400px', '340px'], //宽高
						  content: html
						});
					});
					
					iconPicker.render({
		                // 选择器，推荐使用input
		                elem: '#setMenuLeft_iconPicker',
		                // 数据类型：fontClass/unicode，推荐使用fontClass
		                type: 'fontClass',
		                // 是否开启搜索：true/false
		                search: false,
		                // 是否开启分页
		                page: true,
		                // 每页显示数量，默认12
		                limit: 8,
		                // 点击回调
		                click: function (data) {
		                	$('#setMenuLeft_iconPicker_input_img').val(data.icon)
		                    // console.log(data);
		                }
		            });
		            
		            /**
		             * 选中图标 （常用于更新时默认选中图标）
		             * @param filter lay-filter
		             * @param iconName 图标名称，自动识别fontClass/unicode
		             */
		            if(oldData){
		            	oldData['opentype']=oldData.type
		            	form.val("setMenuLeft_addMenu", oldData)
		            	// console.log(oldData)
		            	//选中图标 （常用于更新时默认选中图标）
      					iconPicker.checkIcon('setMenuLeft_iconPicker', oldData.img);
		            }else{
		            	iconPicker.checkIcon('setMenuLeft_iconPicker', '无');
		            }
		            
		            //监听删除图标
		            $('#setMenuLeft_del_img').click(function(){
		            	// console.log('清除图标');
		            	$('#setMenuLeft_iconPicker_input_img').val('');
		            	layer.msg('已清除图标')
		            	iconPicker.checkIcon('setMenuLeft_iconPicker', '无');
		            })
				}
				
				
			})
			//console.log($data)
			
		</script>
